from PyQt4.QtGui import QWidget,QVBoxLayout,QTableView,QHeaderView
from PyQt4 import QtCore
class KStocksTableView(QTableView):
	ReportSelctionChanged=QtCore.pyqtSignal(str)
	# represent the model of stocks table: (row denotes symbol and col denots feature)
	def __init__(self, parent=None):
		super(KStocksTableView, self).__init__(parent)	
		self.setSortingEnabled(True)
		self.setShowGrid(True)
		self.setSelectionBehavior(self.SelectRows)
		self.setSelectionMode(self.SingleSelection)
		self.setEditTriggers(self.NoEditTriggers)
		self.horizontalHeader().setResizeMode(QHeaderView.Stretch)
		self.doubleClicked.connect(self.OnDoubleClicked)


	def keyPressEvent(self,evt):
		if evt.key()==QtCore.Qt.Key_Return:
			cindex=self.currentIndex()
			if cindex:
				self.Jump2ChartByProxyIndex(cindex)
		QTableView.keyPressEvent(self,evt)
		
	def OnDoubleClicked(self,proxyindex):
		# jump to chart view when double cliked on a row
		self.Jump2ChartByProxyIndex(proxyindex)

	def Jump2ChartByProxyIndex(self,proxyindex):
		# jump to chart view based on given proxyindex
		pmodel=self.model().sourceModel()
		actualindex=self.model().mapToSource(proxyindex)
		symbol=self.model().sourceModel().item(actualindex.row(),0).data(QtCore.Qt.DisplayRole)
		symbol=str(symbol.toString())
		print(symbol)
		# emit signal, notify reportwidget->kstocklistbrowsewidget->kcentralwidget->KSingleChartWidget
		self.ReportSelctionChanged.emit(symbol)
		
	def selectionChanged(self,selecteditem,deselecteditem):
		QTableView.selectionChanged(self,selecteditem,deselecteditem)




